<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class ShopAttrTypeModel extends Model
{
    //定义一个表名
    protected $table = 'shop_attr_type';
    //定义主键
    protected $primaryKey = 'id';

    //多对多规格类型(连衣裙)拥有的规格(颜色,尺寸)
    public function attrkey()
    {
    	return $this->belongsToMany('App\Model\ShopAttrKeyModel','shop_attr_tkv','tid','Kid');
    }

    //获取规格名
	static function attrname($id){
		$str = '';
		$kid = ShopAttrTkvModel::where('tid',$id)->get();
		$kid_num = ShopAttrTkvModel::where('tid',$id)->count();
		if($kid_num > 0){
			$arr = '';
			foreach($kid as $k=>$v){
				$arr .= $v['kid'].",";
			}
			$arr = explode(',',trim($arr,','));
			$arr = array_unique($arr);
			foreach($arr as $k=>$v){
				$str .= ShopAttrKeyModel::where('id',$v)->value('kname').",";
			}
		}
		return $str;
	}


}
